package com.kaoshi.controller;

import com.github.pagehelper.PageHelper;
import com.kaoshi.dao.DictDao;
import com.kaoshi.dao.KemuDao;
import com.kaoshi.dao.Test3Dao;
import com.kaoshi.module.Dict;
import com.kaoshi.module.Kemu;
import com.kaoshi.module.Test3;
import com.kaoshi.util.DataEntity;
import com.kaoshi.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/admin/test3")
@Controller
public class Test3Controller {

    @Autowired
    private Test3Dao dao;
    @Autowired
    private KemuDao kdao;
	@Autowired
	private DictDao dictDao;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(HttpServletRequest request) {
        return "test3/index";
    }

    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    @ResponseBody
    public String getList(@RequestParam(required = false, defaultValue = "1") int page, @RequestParam(required = false, defaultValue = "10") int limit) {
        PageHelper.startPage(page, limit, true);
        Example example = new Example(Test3.class);
        List<Test3> list = dao.selectByExample(example);
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("msg", "");
        result.put("count", dao.selectByExample(example).size());
        result.put("data", list);
        String json = JsonUtils.objectToJson(result);
        return json;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addGET(HttpServletRequest request) {
        List<Kemu> kemulist = kdao.selectAll();
        request.setAttribute("kemulist", kemulist);
		// 获取设置分值信息
		addScoreList(request);
        return "test3/add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public DataEntity addPOST(Test3 data) {
        Kemu k = kdao.selectByPrimaryKey(data.getKid());
        data.setKname(k.getName());
        Integer res = dao.insert(data);
        if (res == 1) {
            return new DataEntity("200", "操作成功", null);
        } else {
            return new DataEntity("400", "操作失败", null);
        }
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String editGET(@RequestParam Integer id, HttpServletRequest request) {
        Test3 data = dao.selectByPrimaryKey(id);
        request.setAttribute("data", data);
        List<Kemu> kemulist = kdao.selectAll();
        request.setAttribute("kemulist", kemulist);
		// 获取设置分值信息
		addScoreList(request);
        return "test3/edit";
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public DataEntity editPOST(Test3 data) {
        Kemu k = kdao.selectByPrimaryKey(data.getKid());
        data.setKname(k.getName());
        Integer res = dao.updateByPrimaryKey(data);
        if (res == 1) {
            return new DataEntity("200", "操作成功", null);
        } else {
            return new DataEntity("400", "操作失败", null);
        }
    }

    @RequestMapping(value = "/del", method = RequestMethod.GET)
    @ResponseBody
    public DataEntity delete(@RequestParam Integer id) {
        Integer res = dao.deleteByPrimaryKey(id);
        if (res == 1) {
            return new DataEntity("200", "操作成功", null);
        } else {
            return new DataEntity("400", "操作失败", null);
        }
    }

	private void addScoreList(HttpServletRequest request) {
		Example example = new Example(Dict.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("type", "score");
		List<Dict> dicts = dictDao.selectByExample(example);
		request.setAttribute("scoreList", dicts);
	}

}